/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FunctionEditor.java
 *
 * Created on 2010-5-30, 10:13:26
 */

package org.free.jshoc;

import java.io.File;
import java.util.HashMap;
import java.util.Stack;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import org.free.jshoc.compute.Evaluator;
import org.free.jshoc.compute.Notifiable;

/**
 *
 * @author juntao
 */
public class Calculator extends javax.swing.JFrame implements Notifiable{

    /** Creates new form FunctionEditor */
    public Calculator() {
        initComponents();
        initEnv();
    }

    public void initEnv(){
        computer = new Evaluator();
        memory = new Stack();
        symTable = new HashMap<String, Object>();
        computer.evalScriptFile(new File("script/system.js"));
        computer.evalScriptFile(new File("script/plot.js"));
        //computer.evalScriptString("plot3d(cs, {start:0.0, step:0.1, stop:1.0}, {start:0.0, step:0.05, stop:1.0})");
        //computer.evalScriptString("plot(sin,{start:-3.0,step:0.1,stop:3.0})");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelButtons = new javax.swing.JPanel();
        textMonitor = new javax.swing.JTextField();
        btnFour = new javax.swing.JButton();
        btnMem = new javax.swing.JButton();
        btnSeven = new javax.swing.JButton();
        btnNine = new javax.swing.JButton();
        btnFive = new javax.swing.JButton();
        btnEight = new javax.swing.JButton();
        btnMultiply = new javax.swing.JButton();
        btnSix = new javax.swing.JButton();
        btnSqrt = new javax.swing.JButton();
        btnDivide = new javax.swing.JButton();
        btnInverse = new javax.swing.JButton();
        btnSubtract = new javax.swing.JButton();
        btnOne = new javax.swing.JButton();
        btnPrecent = new javax.swing.JButton();
        btnThree = new javax.swing.JButton();
        btnTwo = new javax.swing.JButton();
        btnFunc = new javax.swing.JButton();
        btnBS = new javax.swing.JButton();
        btnCE = new javax.swing.JButton();
        btnC = new javax.swing.JButton();
        btnMC = new javax.swing.JButton();
        btnMS = new javax.swing.JButton();
        btnMR = new javax.swing.JButton();
        btnSign = new javax.swing.JButton();
        btnDot = new javax.swing.JButton();
        btnPlus = new javax.swing.JButton();
        btnEqual = new javax.swing.JButton();
        btnZero = new javax.swing.JButton();
        btnVar = new javax.swing.JButton();
        btnQuoto = new javax.swing.JButton();
        menubar = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        menuItemLoad = new javax.swing.JMenuItem();
        menuItemExit = new javax.swing.JMenuItem();
        menuEdit = new javax.swing.JMenu();
        menuItemCut = new javax.swing.JMenuItem();
        menuItemCopy = new javax.swing.JMenuItem();
        menuItemPaste = new javax.swing.JMenuItem();
        menuPlot = new javax.swing.JMenu();
        menuItemPlot2d = new javax.swing.JMenuItem();
        menuItemPlot3d = new javax.swing.JMenuItem();
        menuHelp = new javax.swing.JMenu();
        menuItemContent = new javax.swing.JMenuItem();
        menuItemAbout = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(Calculator.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        panelButtons.setName("panelButtons"); // NOI18N

        textMonitor.setFont(resourceMap.getFont("textMonitor.font")); // NOI18N
        textMonitor.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        textMonitor.setText(resourceMap.getString("textMonitor.text")); // NOI18N
        textMonitor.setName("textMonitor"); // NOI18N

        btnFour.setText(resourceMap.getString("btnFour.text")); // NOI18N
        btnFour.setName("btnFour"); // NOI18N
        btnFour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFourActionPerformed(evt);
            }
        });

        btnMem.setText(resourceMap.getString("btnMem.text")); // NOI18N
        btnMem.setName("btnMem"); // NOI18N
        btnMem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMemActionPerformed(evt);
            }
        });

        btnSeven.setText(resourceMap.getString("btnSeven.text")); // NOI18N
        btnSeven.setName("btnSeven"); // NOI18N
        btnSeven.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSevenActionPerformed(evt);
            }
        });

        btnNine.setText(resourceMap.getString("btnNine.text")); // NOI18N
        btnNine.setName("btnNine"); // NOI18N
        btnNine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNineActionPerformed(evt);
            }
        });

        btnFive.setText(resourceMap.getString("btnFive.text")); // NOI18N
        btnFive.setName("btnFive"); // NOI18N
        btnFive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFiveActionPerformed(evt);
            }
        });

        btnEight.setText(resourceMap.getString("btnEight.text")); // NOI18N
        btnEight.setName("btnEight"); // NOI18N
        btnEight.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEightActionPerformed(evt);
            }
        });

        btnMultiply.setText(resourceMap.getString("btnMultiply.text")); // NOI18N
        btnMultiply.setName("btnMultiply"); // NOI18N
        btnMultiply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMultiplyActionPerformed(evt);
            }
        });

        btnSix.setText(resourceMap.getString("btnSix.text")); // NOI18N
        btnSix.setName("btnSix"); // NOI18N
        btnSix.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSixActionPerformed(evt);
            }
        });

        btnSqrt.setText(resourceMap.getString("btnSqrt.text")); // NOI18N
        btnSqrt.setName("btnSqrt"); // NOI18N
        btnSqrt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSqrtActionPerformed(evt);
            }
        });

        btnDivide.setText(resourceMap.getString("btnDivide.text")); // NOI18N
        btnDivide.setName("btnDivide"); // NOI18N
        btnDivide.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDivideActionPerformed(evt);
            }
        });

        btnInverse.setText(resourceMap.getString("btnInverse.text")); // NOI18N
        btnInverse.setName("btnInverse"); // NOI18N
        btnInverse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInverseActionPerformed(evt);
            }
        });

        btnSubtract.setText(resourceMap.getString("btnSubtract.text")); // NOI18N
        btnSubtract.setName("btnSubtract"); // NOI18N
        btnSubtract.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubtractActionPerformed(evt);
            }
        });

        btnOne.setText(resourceMap.getString("btnOne.text")); // NOI18N
        btnOne.setName("btnOne"); // NOI18N
        btnOne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOneActionPerformed(evt);
            }
        });

        btnPrecent.setText(resourceMap.getString("btnPrecent.text")); // NOI18N
        btnPrecent.setName("btnPrecent"); // NOI18N
        btnPrecent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrecentActionPerformed(evt);
            }
        });

        btnThree.setText(resourceMap.getString("btnThree.text")); // NOI18N
        btnThree.setName("btnThree"); // NOI18N
        btnThree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThreeActionPerformed(evt);
            }
        });

        btnTwo.setText(resourceMap.getString("btnTwo.text")); // NOI18N
        btnTwo.setName("btnTwo"); // NOI18N
        btnTwo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTwoActionPerformed(evt);
            }
        });

        btnFunc.setText(resourceMap.getString("btnFunc.text")); // NOI18N
        btnFunc.setName("btnFunc"); // NOI18N
        btnFunc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFuncActionPerformed(evt);
            }
        });

        btnBS.setText(resourceMap.getString("btnBS.text")); // NOI18N
        btnBS.setName("btnBS"); // NOI18N
        btnBS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBSActionPerformed(evt);
            }
        });

        btnCE.setText(resourceMap.getString("btnCE.text")); // NOI18N
        btnCE.setName("btnCE"); // NOI18N

        btnC.setText(resourceMap.getString("btnC.text")); // NOI18N
        btnC.setName("btnC"); // NOI18N
        btnC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCActionPerformed(evt);
            }
        });

        btnMC.setText(resourceMap.getString("btnMC.text")); // NOI18N
        btnMC.setName("btnMC"); // NOI18N
        btnMC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMCActionPerformed(evt);
            }
        });

        btnMS.setText(resourceMap.getString("btnMS.text")); // NOI18N
        btnMS.setName("btnMS"); // NOI18N
        btnMS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMSActionPerformed(evt);
            }
        });

        btnMR.setText(resourceMap.getString("btnMR.text")); // NOI18N
        btnMR.setName("btnMR"); // NOI18N
        btnMR.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMRActionPerformed(evt);
            }
        });

        btnSign.setText(resourceMap.getString("btnSign.text")); // NOI18N
        btnSign.setName("btnSign"); // NOI18N
        btnSign.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSignActionPerformed(evt);
            }
        });

        btnDot.setText(resourceMap.getString("btnDot.text")); // NOI18N
        btnDot.setName("btnDot"); // NOI18N
        btnDot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDotActionPerformed(evt);
            }
        });

        btnPlus.setText(resourceMap.getString("btnPlus.text")); // NOI18N
        btnPlus.setName("btnPlus"); // NOI18N
        btnPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlusActionPerformed(evt);
            }
        });

        btnEqual.setText(resourceMap.getString("btnEqual.text")); // NOI18N
        btnEqual.setName("btnEqual"); // NOI18N
        btnEqual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEqualActionPerformed(evt);
            }
        });

        btnZero.setText(resourceMap.getString("btnZero.text")); // NOI18N
        btnZero.setName("btnZero"); // NOI18N
        btnZero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnZeroActionPerformed(evt);
            }
        });

        btnVar.setText(resourceMap.getString("btnVar.text")); // NOI18N
        btnVar.setName("btnVar"); // NOI18N
        btnVar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVarActionPerformed(evt);
            }
        });

        btnQuoto.setText(resourceMap.getString("btnQuoto.text")); // NOI18N
        btnQuoto.setName("btnQuoto"); // NOI18N
        btnQuoto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuotoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelButtonsLayout = new javax.swing.GroupLayout(panelButtons);
        panelButtons.setLayout(panelButtonsLayout);
        panelButtonsLayout.setHorizontalGroup(
            panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textMonitor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, panelButtonsLayout.createSequentialGroup()
                        .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnMem, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
                            .addComponent(btnMS, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
                            .addComponent(btnMR, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
                            .addComponent(btnMC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnFunc))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelButtonsLayout.createSequentialGroup()
                                .addComponent(btnFour, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnFive, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSix, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnMultiply, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnPrecent, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelButtonsLayout.createSequentialGroup()
                                .addComponent(btnOne, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnTwo, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnThree, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSubtract, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnInverse, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelButtonsLayout.createSequentialGroup()
                                .addComponent(btnZero, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSign, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnDot, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnEqual, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(panelButtonsLayout.createSequentialGroup()
                                .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(btnVar)
                                    .addComponent(btnSeven, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(btnEight, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnQuoto, 0, 0, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(btnBS, 0, 0, Short.MAX_VALUE)
                                    .addComponent(btnNine, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(btnCE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnDivide, javax.swing.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(btnC, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnSqrt, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))))))
                .addContainerGap())
        );

        panelButtonsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnDivide, btnDot, btnEight, btnEqual, btnFive, btnFour, btnInverse, btnMultiply, btnNine, btnOne, btnPlus, btnPrecent, btnSeven, btnSign, btnSix, btnSqrt, btnSubtract, btnThree, btnTwo, btnZero});

        panelButtonsLayout.setVerticalGroup(
            panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textMonitor, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(panelButtonsLayout.createSequentialGroup()
                        .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnC)
                            .addComponent(btnCE)
                            .addComponent(btnBS)
                            .addComponent(btnQuoto))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSeven)
                            .addComponent(btnEight)
                            .addComponent(btnNine)
                            .addComponent(btnDivide)
                            .addComponent(btnSqrt))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnFour)
                            .addComponent(btnFive)
                            .addComponent(btnSix)
                            .addComponent(btnMultiply)
                            .addComponent(btnPrecent))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnOne)
                            .addComponent(btnTwo)
                            .addComponent(btnThree)
                            .addComponent(btnSubtract)
                            .addComponent(btnInverse))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnZero)
                            .addComponent(btnSign, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnDot)
                            .addComponent(btnPlus)
                            .addComponent(btnEqual)))
                    .addGroup(panelButtonsLayout.createSequentialGroup()
                        .addGroup(panelButtonsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnFunc)
                            .addComponent(btnVar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMC)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMR)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMS)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnMem)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelButtonsLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnBS, btnC, btnCE, btnDivide, btnDot, btnEight, btnEqual, btnFive, btnFour, btnFunc, btnInverse, btnMC, btnMR, btnMS, btnMem, btnMultiply, btnNine, btnOne, btnPlus, btnPrecent, btnQuoto, btnSeven, btnSign, btnSix, btnSqrt, btnSubtract, btnThree, btnTwo, btnVar, btnZero});

        menubar.setName("menubar"); // NOI18N

        menuFile.setText(resourceMap.getString("menuFile.text")); // NOI18N
        menuFile.setName("menuFile"); // NOI18N

        menuItemLoad.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        menuItemLoad.setText(resourceMap.getString("menuItemLoad.text")); // NOI18N
        menuItemLoad.setName("menuItemLoad"); // NOI18N
        menuItemLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemLoadActionPerformed(evt);
            }
        });
        menuFile.add(menuItemLoad);

        menuItemExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        menuItemExit.setText(resourceMap.getString("menuItemExit.text")); // NOI18N
        menuItemExit.setName("menuItemExit"); // NOI18N
        menuItemExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemExitActionPerformed(evt);
            }
        });
        menuFile.add(menuItemExit);

        menubar.add(menuFile);

        menuEdit.setText(resourceMap.getString("menuEdit.text")); // NOI18N
        menuEdit.setName("menuEdit"); // NOI18N

        menuItemCut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        menuItemCut.setText(resourceMap.getString("menuItemCut.text")); // NOI18N
        menuItemCut.setName("menuItemCut"); // NOI18N
        menuEdit.add(menuItemCut);

        menuItemCopy.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        menuItemCopy.setText(resourceMap.getString("menuItemCopy.text")); // NOI18N
        menuItemCopy.setName("menuItemCopy"); // NOI18N
        menuEdit.add(menuItemCopy);

        menuItemPaste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        menuItemPaste.setText(resourceMap.getString("menuItemPaste.text")); // NOI18N
        menuItemPaste.setName("menuItemPaste"); // NOI18N
        menuEdit.add(menuItemPaste);

        menubar.add(menuEdit);

        menuPlot.setText(resourceMap.getString("menuPlot.text")); // NOI18N
        menuPlot.setName("menuPlot"); // NOI18N
        menuPlot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuPlotActionPerformed(evt);
            }
        });

        menuItemPlot2d.setText(resourceMap.getString("menuItemPlot2d.text")); // NOI18N
        menuItemPlot2d.setName("menuItemPlot2d"); // NOI18N
        menuItemPlot2d.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemPlot2dActionPerformed(evt);
            }
        });
        menuPlot.add(menuItemPlot2d);

        menuItemPlot3d.setText(resourceMap.getString("menuItemPlot3d.text")); // NOI18N
        menuItemPlot3d.setName("menuItemPlot3d"); // NOI18N
        menuItemPlot3d.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemPlot3dActionPerformed(evt);
            }
        });
        menuPlot.add(menuItemPlot3d);

        menubar.add(menuPlot);

        menuHelp.setText(resourceMap.getString("menuHelp.text")); // NOI18N
        menuHelp.setName("menuHelp"); // NOI18N

        menuItemContent.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        menuItemContent.setText(resourceMap.getString("menuItemContent.text")); // NOI18N
        menuItemContent.setName("menuItemContent"); // NOI18N
        menuHelp.add(menuItemContent);

        menuItemAbout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
        menuItemAbout.setText(resourceMap.getString("menuItemAbout.text")); // NOI18N
        menuItemAbout.setName("menuItemAbout"); // NOI18N
        menuItemAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemAboutActionPerformed(evt);
            }
        });
        menuHelp.add(menuItemAbout);

        menubar.add(menuHelp);

        setJMenuBar(menubar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelButtons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFuncActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFuncActionPerformed
        // TODO add your handling code here:
        FunctionEditor fe = new FunctionEditor();
        fe.setLocationRelativeTo(null);
        fe.setVisible(true);
    }//GEN-LAST:event_btnFuncActionPerformed

    private void btnMemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMemActionPerformed
        // TODO add your handling code here:
        MemoryFrame mf = new MemoryFrame(memory);
        mf.setLocationRelativeTo(null);
        mf.setVisible(true);
    }//GEN-LAST:event_btnMemActionPerformed

    private void menuItemLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemLoadActionPerformed
        // TODO add your handling code here:
        //this.fileChooser.setVisible(true);
        JFileChooser chooser = new JFileChooser();
        int ret = chooser.showOpenDialog(this);

        if (ret != JFileChooser.APPROVE_OPTION) {
            return;
        }

        File f = chooser.getSelectedFile();
        computer.evalScriptFile(f);
    }//GEN-LAST:event_menuItemLoadActionPerformed

    private void btnEqualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEqualActionPerformed
        // TODO add your handling code here:
        String expr = textMonitor.getText();
        Object result = computer.evalScriptString(expr);
        textMonitor.setText(result.toString());
    }//GEN-LAST:event_btnEqualActionPerformed

    private void btnCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCActionPerformed
        // TODO add your handling code here:
        textMonitor.setText("");
    }//GEN-LAST:event_btnCActionPerformed

    private void btnBSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBSActionPerformed
        // TODO add your handling code here:
        String text = textMonitor.getText();
        int len = text.length();
        if(text != null && len > 0){
            textMonitor.setText(text.substring(0, len-1));
        }
    }//GEN-LAST:event_btnBSActionPerformed

    private void btnMSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMSActionPerformed
        // TODO add your handling code here:
        String expr = textMonitor.getText();
        Object obj = null;
        if(expr != null){
            obj = computer.evalScriptString(expr);
            memory.push(obj);

            symTable.put("_tos_", obj);
        }
    }//GEN-LAST:event_btnMSActionPerformed

    private void btnMRActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMRActionPerformed
        // TODO add your handling code here:
        if(memory != null && memory.size() > 0){
            Object obj = memory.peek();
            String org = textMonitor.getText();
            textMonitor.setText(org + obj.toString());
        }
    }//GEN-LAST:event_btnMRActionPerformed

    private void btnMCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMCActionPerformed
        // TODO add your handling code here:
        if(memory != null){
            memory.clear();
            symTable.put("_tos_", null);
        }
    }//GEN-LAST:event_btnMCActionPerformed

    private void btnSevenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSevenActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"7");
    }//GEN-LAST:event_btnSevenActionPerformed

    private void btnEightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEightActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"8");
    }//GEN-LAST:event_btnEightActionPerformed

    private void btnNineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNineActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"9");
    }//GEN-LAST:event_btnNineActionPerformed

    private void btnDivideActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDivideActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"/");
    }//GEN-LAST:event_btnDivideActionPerformed

    private void btnSqrtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSqrtActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText("Math.sqrt("+org+")");
    }//GEN-LAST:event_btnSqrtActionPerformed

    private void btnFourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFourActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"4");
    }//GEN-LAST:event_btnFourActionPerformed

    private void btnFiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFiveActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"5");
    }//GEN-LAST:event_btnFiveActionPerformed

    private void btnSixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSixActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"6");
    }//GEN-LAST:event_btnSixActionPerformed

    private void btnMultiplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMultiplyActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"*");
    }//GEN-LAST:event_btnMultiplyActionPerformed

    private void btnPrecentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrecentActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        Object obj = null;
        if(org != null){
            obj = computer.evalScriptString(org+"*100");
            textMonitor.setText(obj.toString()+"%");
        }
    }//GEN-LAST:event_btnPrecentActionPerformed

    private void btnOneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOneActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"1");
    }//GEN-LAST:event_btnOneActionPerformed

    private void btnTwoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTwoActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"2");
    }//GEN-LAST:event_btnTwoActionPerformed

    private void btnThreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThreeActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"3");
    }//GEN-LAST:event_btnThreeActionPerformed

    private void btnSubtractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubtractActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"-");
    }//GEN-LAST:event_btnSubtractActionPerformed

    private void btnInverseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInverseActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        if(org != null){
            Object obj = computer.evalScriptString("1/"+org);
            textMonitor.setText(obj.toString());
        }
    }//GEN-LAST:event_btnInverseActionPerformed

    private void btnZeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnZeroActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"0");
    }//GEN-LAST:event_btnZeroActionPerformed

    private void btnSignActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSignActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        if(org.startsWith("-")){
            textMonitor.setText(org.substring(1));
        }else{
            textMonitor.setText("-"+org);
        }
    }//GEN-LAST:event_btnSignActionPerformed

    private void btnDotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDotActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+".");
    }//GEN-LAST:event_btnDotActionPerformed

    private void btnPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlusActionPerformed
        // TODO add your handling code here:
        String org = textMonitor.getText();
        textMonitor.setText(org+"+");
    }//GEN-LAST:event_btnPlusActionPerformed

    private void menuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemExitActionPerformed
        // TODO add your handling code here:
        int y = JOptionPane.showConfirmDialog(null, "Confirm exit",
                        "Confirm Exit Dialog", JOptionPane.YES_NO_OPTION);
        if (y == JOptionPane.YES_OPTION) {
                System.exit(0);
        }
    }//GEN-LAST:event_menuItemExitActionPerformed

    private void menuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemAboutActionPerformed
        // TODO add your handling code here:
        AboutBox ab = new AboutBox(this);
        ab.setLocationRelativeTo(this);
        ab.setVisible(true);
    }//GEN-LAST:event_menuItemAboutActionPerformed

    private void menuPlotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuPlotActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuPlotActionPerformed

    private void menuItemPlot2dActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemPlot2dActionPerformed
        // TODO add your handling code here:
        String expr = textMonitor.getText();
        computer.evalScriptString("plot2d"+expr);
    }//GEN-LAST:event_menuItemPlot2dActionPerformed

    private void menuItemPlot3dActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemPlot3dActionPerformed
        // TODO add your handling code here:
        String expr = textMonitor.getText();
        computer.evalScriptString("plot3d"+expr);
    }//GEN-LAST:event_menuItemPlot3dActionPerformed

    private void btnVarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVarActionPerformed
        // TODO add your handling code here:
        VariableFrame vf = new VariableFrame(this, symTable);
        vf.setLocationRelativeTo(this);
        vf.setVisible(true);
    }//GEN-LAST:event_btnVarActionPerformed

    private void btnQuotoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuotoActionPerformed
        // TODO add your handling code here:
        String normal = textMonitor.getSelectedText();
        if(normal == null || normal.length() == 0){
            return;
        }else{
            String all = textMonitor.getText();
            String res = all.replace(normal, "("+normal+")");
            textMonitor.setText(res);
        }
    }//GEN-LAST:event_btnQuotoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        String className = UIManager.getSystemLookAndFeelClassName();
        try {
            UIManager.setLookAndFeel(className);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Calculator calc = new Calculator();
                calc.setLocationRelativeTo(null);
                calc.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBS;
    private javax.swing.JButton btnC;
    private javax.swing.JButton btnCE;
    private javax.swing.JButton btnDivide;
    private javax.swing.JButton btnDot;
    private javax.swing.JButton btnEight;
    private javax.swing.JButton btnEqual;
    private javax.swing.JButton btnFive;
    private javax.swing.JButton btnFour;
    private javax.swing.JButton btnFunc;
    private javax.swing.JButton btnInverse;
    private javax.swing.JButton btnMC;
    private javax.swing.JButton btnMR;
    private javax.swing.JButton btnMS;
    private javax.swing.JButton btnMem;
    private javax.swing.JButton btnMultiply;
    private javax.swing.JButton btnNine;
    private javax.swing.JButton btnOne;
    private javax.swing.JButton btnPlus;
    private javax.swing.JButton btnPrecent;
    private javax.swing.JButton btnQuoto;
    private javax.swing.JButton btnSeven;
    private javax.swing.JButton btnSign;
    private javax.swing.JButton btnSix;
    private javax.swing.JButton btnSqrt;
    private javax.swing.JButton btnSubtract;
    private javax.swing.JButton btnThree;
    private javax.swing.JButton btnTwo;
    private javax.swing.JButton btnVar;
    private javax.swing.JButton btnZero;
    private javax.swing.JMenu menuEdit;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenu menuHelp;
    private javax.swing.JMenuItem menuItemAbout;
    private javax.swing.JMenuItem menuItemContent;
    private javax.swing.JMenuItem menuItemCopy;
    private javax.swing.JMenuItem menuItemCut;
    private javax.swing.JMenuItem menuItemExit;
    private javax.swing.JMenuItem menuItemLoad;
    private javax.swing.JMenuItem menuItemPaste;
    private javax.swing.JMenuItem menuItemPlot2d;
    private javax.swing.JMenuItem menuItemPlot3d;
    private javax.swing.JMenu menuPlot;
    private javax.swing.JMenuBar menubar;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JTextField textMonitor;
    // End of variables declaration//GEN-END:variables

    private Evaluator computer;
    private Stack memory;
    private HashMap<String, Object> symTable;

    public void callback(Object obj) {
        computer.evalScriptString(obj.toString());
    }
}
